import {
    request
} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
// pages/order/order.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: [{
                id: 0,
                value: "全部",
                isActive: true
            },
            {
                id: 1,
                value: "待付款",
                isActive: false
            },
            {
                id: 2,
                value: "待收货",
                isActive: false
            },
            {
                id: 3,
                value: "退货/退款",
                isActive: false
            },
        ],
        orders: [],
    },
    // 根据标题索引激活选中标题数组
    changeTileByIndex(index) {
        // 修改原数组 isActive 变为 true
        let {
            tabs
        } = this.data
        tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
        // 赋值
        this.setData({
            tabs
        })
    },
    //监听子组件触发的回调函数
    handleTabsItemChange(e) {
        // 获取被点击的标题索引
        const {
            index
        } = e.detail
        this.changeTileByIndex(index);
        // 重新发送请求
        this.getOrders(index+1);
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (options) {
        // 判断token是否存在 不存在跳转授权获取
        const token = wx.getStorageSync('token');
        if (!token) {
            wx.navigateTo({
                url: '/pages/auth/auth',
            });
            return;
        }
        // options为undefined onshow无法在形参上接收参数的 onload的可以
        // 获取当前小程序的页面栈-数组 长度最大为10
        let pages = getCurrentPages();
        // 数组中索引最大的就是当前页面
        let currentPage = pages[pages.length - 1];
        // 获取url上的type参数
        let {
            type
        } = currentPage.options;
        // 根据type值改变选中tab的选项
        this.changeTileByIndex(type-1);

        // 获取订单列表
        this.getOrders(type);
    },
    // 获取订单数据
    async getOrders(type) {
        const res = await request({
            url: "/my/orders/all",
            data: {
                type
            }
        });
        let orders = res.orders || [{
                "order_id": 428,
                "user_id": 23,
                "order_number": "HMDD20190802000000000428",
                "order_price": 27998,
                "order_pay": "0",
                "is_send": "否",
                "trade_no": "",
                "order_fapiao_title": "个人",
                "order_fapiao_company": "",
                "order_fapiao_content": "",
                "consignee_addr": "广东省广州市海珠区新港中路397号",
                "pay_status": "1",
                "create_time": 1564731518,
                "update_time": 1564731518,
                "order_detail": null,
                "goods": [{
                        "id": 717,
                        "order_id": 428,
                        "goods_id": 43986,
                        "goods_price": 13999,
                        "goods_number": 1,
                        "goods_total_price": 13999,
                        "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
                        "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
                    },
                    {
                        "id": 717,
                        "order_id": 428,
                        "goods_id": 43986,
                        "goods_price": 13999,
                        "goods_number": 1,
                        "goods_total_price": 13999,
                        "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
                        "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
                    }
                ],
                "total_count": 2,
                "total_price": 27998
            },
            {
                "order_id": 428,
                "user_id": 23,
                "order_number": "HMDD20190802000000000428",
                "order_price": 13999,
                "order_pay": "0",
                "is_send": "否",
                "trade_no": "",
                "order_fapiao_title": "个人",
                "order_fapiao_company": "",
                "order_fapiao_content": "",
                "consignee_addr": "广东省广州市海珠区新港中路397号",
                "pay_status": "1",
                "create_time": 1564731518,
                "update_time": 1564731518,
                "order_detail": null,
                "goods": [{
                    "id": 717,
                    "order_id": 428,
                    "goods_id": 43986,
                    "goods_price": 13999,
                    "goods_number": 1,
                    "goods_total_price": 13999,
                    "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
                    "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
                }],
                "total_count": 1,
                "total_price": 13999
            },
            {
                "order_id": 428,
                "user_id": 23,
                "order_number": "HMDD20190802000000000428",
                "order_price": 27998,
                "order_pay": "0",
                "is_send": "否",
                "trade_no": "",
                "order_fapiao_title": "个人",
                "order_fapiao_company": "",
                "order_fapiao_content": "",
                "consignee_addr": "广东省广州市海珠区新港中路397号",
                "pay_status": "1",
                "create_time": 1564731518,
                "update_time": 1564731518,
                "order_detail": null,
                "goods": [{
                        "id": 717,
                        "order_id": 428,
                        "goods_id": 43986,
                        "goods_price": 13999,
                        "goods_number": 1,
                        "goods_total_price": 13999,
                        "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
                        "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
                    },
                    {
                        "id": 717,
                        "order_id": 428,
                        "goods_id": 43986,
                        "goods_price": 13999,
                        "goods_number": 1,
                        "goods_total_price": 13999,
                        "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
                        "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
                    }
                ],
                "total_count": 2,
                "total_price": 27998
            },
            {
                "order_id": 428,
                "user_id": 23,
                "order_number": "HMDD20190802000000000428",
                "order_price": 13999,
                "order_pay": "0",
                "is_send": "否",
                "trade_no": "",
                "order_fapiao_title": "个人",
                "order_fapiao_company": "",
                "order_fapiao_content": "",
                "consignee_addr": "广东省广州市海珠区新港中路397号",
                "pay_status": "1",
                "create_time": 1564731518,
                "update_time": 1564731518,
                "order_detail": null,
                "goods": [{
                    "id": 717,
                    "order_id": 428,
                    "goods_id": 43986,
                    "goods_price": 13999,
                    "goods_number": 1,
                    "goods_total_price": 13999,
                    "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
                    "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
                }],
                "total_count": 1,
                "total_price": 13999
            },
            {
                "order_id": 428,
                "user_id": 23,
                "order_number": "HMDD20190802000000000428",
                "order_price": 27998,
                "order_pay": "0",
                "is_send": "否",
                "trade_no": "",
                "order_fapiao_title": "个人",
                "order_fapiao_company": "",
                "order_fapiao_content": "",
                "consignee_addr": "广东省广州市海珠区新港中路397号",
                "pay_status": "1",
                "create_time": 1564731518,
                "update_time": 1564731518,
                "order_detail": null,
                "goods": [{
                        "id": 717,
                        "order_id": 428,
                        "goods_id": 43986,
                        "goods_price": 13999,
                        "goods_number": 1,
                        "goods_total_price": 13999,
                        "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
                        "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
                    },
                    {
                        "id": 717,
                        "order_id": 428,
                        "goods_id": 43986,
                        "goods_price": 13999,
                        "goods_number": 1,
                        "goods_total_price": 13999,
                        "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
                        "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg"
                    }
                ],
                "total_count": 2,
                "total_price": 27998
            }
        ];
        orders = orders.map(v => ({ ...v, create_time_cn:(new Date(v.create_time*1000).toLocaleString()) }));
        console.log(orders)
        this.setData({
            orders
        });
    }
})